package com.cn.kabalifemerchants.ui.hall;

import android.content.Context;
import android.content.DialogInterface;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.cn.kabalifemerchants.R;
import com.cn.kabalifemerchants.base.LazyFragment;
import com.cn.kabalifemerchants.config.CustomRefreshListener;
import com.cn.kabalifemerchants.config.LoginHelper;
import com.cn.kabalifemerchants.config.RefreshHelper;
import com.cn.kabalifemerchants.ui.hall.adapter.HallListAdapter;
import com.cn.kabalifemerchants.ui.hall.bean.HallListBean;
import com.cn.kabalifemerchants.ui.login.bean.MsgBean;
import com.cn.kabalifemerchants.utils.RxToast;
import com.cn.kabalifemerchants.view.NotDataView;
import com.kongzue.dialog.v2.SelectDialog;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 晓林 on 2020/10/14.
 * function:
 */
public class FragmenHall extends LazyFragment<FragmenHallsView, FragmenHallsPresenter> implements FragmenHallsView {


    @BindView(R.id.rv_data)
    RecyclerView rvData;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    List<HallListBean.ResultBean.DataBean> mList;
    HallListAdapter mAdapter;
    private View notDataView;

    int itemPosition = -1;

    @Override
    protected FragmenHallsPresenter createPresenter() {
        return new FragmenHallsPresenter();
    }

    @Override
    protected void onUserVisible() {
        if (mList != null && mList.size() == 0) {
            pagehttp = 1;
            getListData();
        }
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_home_hall;

    }


    @Override
    public void initView(View view) {
        super.initView(view);
        ButterKnife.bind(this, view);
        mList = new ArrayList<>();
        LinearLayoutManager layoutManager3 = new LinearLayoutManager(getMe());
        layoutManager3.setOrientation(LinearLayoutManager.VERTICAL);
        layoutManager3.setSmoothScrollbarEnabled(true);
        layoutManager3.setAutoMeasureEnabled(true);
        rvData.setLayoutManager(layoutManager3);
        rvData.setNestedScrollingEnabled(false);
        //创建适配器
        mAdapter = new HallListAdapter(R.layout.halls_item, mList);
        //给RecyclerView设置适配器
        rvData.setAdapter(mAdapter);
        notDataView = NotDataView.notDataView(getActivity(), rvData, refreshLayout);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                //todo 跳转详情
//                Bundle bundle = new Bundle();
//                bundle.putInt("id", mAdapter.getItem(position).getId());
//                $startActivity(IntegralCommodityDetailsActivity.class, bundle);
            }
        });
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.tv_btn_next:
                        SelectDialog.show(getMe(), "提示", "确定要抢该订单吗？", "确认", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                itemPosition = position;
                                mPresenter.onHallsgRabBingData(LoginHelper.getLonginData().getResult(), mAdapter.getItem(position).getId());
                            }
                        }, "取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        });
                        break;
                }
            }
        });
        initRefreshLayout();
    }

    /**
     * 刷新
     */
    public void initRefreshLayout() {
        RefreshHelper.initRefreshLayout(refreshLayout, new CustomRefreshListener() {
            @Override
            public void _onRefresh(RefreshLayout refreshLayout) {
                pagehttp = 1;
                getListData();
            }

            @Override
            public void _onLoadMore(RefreshLayout refreshLayout) {
                pagehttp++;
                if (null != mList) {
                    getListData();
                }
            }
        });
    }

    private void getListData() {
        mPresenter.onHallsListData(LoginHelper.getLonginData().getResult(), pagehttp, pageNum);
    }

    @Override
    public void onReLoggedIn(String msg) {
        RxToast.error(msg);
        LoginHelper.againGoToLoginActivity(getMe());
    }

    @Override
    public void onError(String msg) {
        RxToast.error(msg);
    }

    @Override
    public Context _getContext() {
        return null;
    }

    @Override
    public void onHallsListSuccess(HallListBean bean) {
        RefreshHelper.finishRefresh(refreshLayout, pagehttp);
        if (pagehttp == 1) {
            if (bean.getResult() != null && bean.getResult().getData().size() > 0) {
                mAdapter.replaceData(bean.getResult().getData());
            } else {
                mAdapter.replaceData(bean.getResult().getData());
                mAdapter.setEmptyView(notDataView);
            }
        } else {
            if (bean.getResult() != null && bean.getResult().getData().size() > 0) {
                mAdapter.addData(bean.getResult().getData());
            }

        }
    }

    @Override
    public void onHallsgRabBingSuccess(MsgBean bean) {
        RxToast.success(bean.getResult());
        mAdapter.remove(itemPosition);
        mAdapter.notifyDataSetChanged();
    }
}


